/**
 * 计算 .table-section 元素可用的高度
 * 即：window.innerHeight 减去其父容器内上下所有兄弟元素的总高度（含 margin）
 * @returns {number} 可用高度（像素），最小为 0
 */
export function calculateTableSectionHeight() {
    const tableSection = document.getElementById("tableRefCustomId");
    if (!tableSection || !tableSection.parentElement) {
        return 0;
    }

    const parentEl = tableSection.parentElement;
    const siblings = Array.from(parentEl.children);
    const tableSectionIndex = siblings.indexOf(tableSection);

    if (tableSectionIndex === -1) return 0;

    let totalAboveHeight = 0;
    let totalBelowHeight = 0;

    // 计算上方兄弟元素的高度（含 margin）
    for (let i = 0; i < tableSectionIndex; i++) {
        const el = siblings[i];
        const styles = window.getComputedStyle(el);
        const marginTop = parseFloat(styles.marginTop) || 0;
        const marginBottom = parseFloat(styles.marginBottom) || 0;
        // 使用 getBoundingClientRect().height 更真实反映渲染高度
        totalAboveHeight += el.getBoundingClientRect().height + marginTop + marginBottom;
    }

    // 计算下方兄弟元素的高度（含 margin）
    for (let i = tableSectionIndex + 1; i < siblings.length; i++) {
        const el = siblings[i];
        const styles = window.getComputedStyle(el);
        const marginTop = parseFloat(styles.marginTop) || 0;
        const marginBottom = parseFloat(styles.marginBottom) || 0;
        totalBelowHeight += el.getBoundingClientRect().height + marginTop + marginBottom;
    }

    const availableHeight = window.innerHeight - totalAboveHeight - totalBelowHeight;

    return Math.max(availableHeight, 0);
}